Author

Tony Duan

install quarto

install quarto CLI

https://quarto.org/docs/get-started/

install quarto pacakge

Code
install.packages("quarto")

check quarto version

in Terminal

Code
Terminal
quarto check

in R

Code
library(quarto)
quarto_version()
[1] '99.9.9'

quarto location

Code
quarto_path()
[1] "/usr/local/bin/quarto"

quarto code chunk

R

```{R}
version
```

Python

```{python}
import sys
print(sys.version)
```

Bash

```{bash}
echo "foo" 
```

calling variable between R and Python

There are R variable: r_var and Python version:python_var

```{r}
r_var=111
```
```{python}
python_var=222
```
```{python}
r.r_var
```
111.0
```{r}
library(reticulate)
py$python_var
```
[1] 222

quarto code chunk option

in YAML header

---
title: "My Document"
execute:
  echo: false
jupyter: python3
---
  • eval: Evaluate the code chunk (if false, just echos the code into the output).

  • echo: Include the source code in output.

  • output: Include the results of executing the code in the output (true, false, or asis to indicate that the output is raw markdown and should not have any of Quarto’s standard enclosing markdown).

  • warning: Include warnings in the output.

  • error: Evaluate the code chunk (if false, just echos the code into the output).

  • include: Catch all for preventing any output (code or results) from being included (e.g. include: false suppresses all output from the code block).

in code chunk

Code
 #| eval: false

code chunk name

{r filename='code chunk name sample'}

Code
code chunk name sample
a=1
a
[1] 1

Rmarkdown

Code
*italics* and _italics_
**bold** and __bold__
superscript^2^
~~strikethrough~~
[link](www.rstudio.com)
Code
*italics* 

italics

Code
**bold**

bold

Code
superscript^2^

superscript2

Code
~~strikethrough~~

strikethrough

Code
`highlight`

highlight

tab

::: {.panel-tabset}

## Tab 1

This is tab 1


## Tab 2

This is tab 2

:::

This is tab 1

This is tab 2

column display

Code
::: columns
::: {.column width="40%"}
this column with 40% width
::: 

::: {.column width="60%"}
this column with 60% width
::: 

::: 

this column with 40% width

this column with 60% width

Add a horizontal scroll bar

  • add hscroll.css file
Code
.hscroll {
  overflow: auto;
  white-space: nowrap;
}
  • add css option in YAML header
Code
---
css: hscroll.css
---
  • add # {.hscroll .scrollable} on the section which want to be Slide bar

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas dolor mauris, mattis non nunc nec, luctus aliquet magna. In tortor ante, aliquam eget felis eu, consectetur congue enim. Pellentesque pellentesque tincidunt lectus quis lacinia. Aliquam dolor sem, aliquet vitae aliquam eget, vehicula ac augue. Pellentesque molestie quam et commodo dignissim. Suspendisse potenti. Sed pellentesque metus non turpis condimentum molestie. Nullam accumsan urna nec luctus rutrum. Nunc dapibus auctor convallis. Praesent ac felis fermentum, venenatis elit vel, venenatis mi. Fusce non tellus risus. Phasellus elementum erat sit amet urna venenatis maximus. Nunc vehicula pretium scelerisque.

Add a vertical scroll bar

images1.png

images2.png

add a output hide bottom at quarto file

step 1. add foldableCodeBlcok.lua into blog root folder

Code
foldableCodeBlcok.lua
function CodeBlock(elem)
  if elem.classes and elem.classes:find("details") then
    local summary = "Code"
    if elem.attributes.summary then
      summary = elem.attributes.summary
    end
    return{
      pandoc.RawBlock(
        "html", "<details><summary>" .. summary .. "</summary>"
      ),
      elem,
      pandoc.RawBlock("html", "</details>")
    }
  end
end

step 2. add following line in _quarto.yml

Code
_quarto.yml
website:
  title: "quarto blog"

filters:
  - foldableCodeBlcok.lua

step 3. put following on code chunk:

{r, attr.output='.details summary="sessionInfo()"'}

Code
sessionInfo()
sessionInfo()
R version 4.4.1 (2024-06-14)
Platform: aarch64-apple-darwin20
Running under: macOS 15.3.2

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Asia/Shanghai
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] reticulate_1.41.0 quarto_1.4.4     

loaded via a namespace (and not attached):
 [1] digest_0.6.37       later_1.4.1         fastmap_1.2.0      
 [4] Matrix_1.7-2        xfun_0.51           lattice_0.22-6     
 [7] knitr_1.49          htmltools_0.5.8.1   png_0.1-8          
[10] rmarkdown_2.29      ps_1.9.0            cli_3.6.4          
[13] processx_3.8.6.9000 grid_4.4.1          compiler_4.4.1     
[16] rstudioapi_0.17.1   tools_4.4.1         evaluate_1.0.3     
[19] Rcpp_1.0.14         yaml_2.3.10         rlang_1.1.5        
[22] jsonlite_2.0.0      htmlwidgets_1.6.4  

render all quarto website

Code
Terminal
quarto render "tidymodel in R"

embed video

embed youtube

Code
<iframe width="640" height="480" src="https://www.youtube.com/embed/nku5zFMZAdU" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen>
</iframe>

embed bilibili

embed local video

Code
{{< video images/vv.mp4 height="600" >}}

embed audio

Code
<figure>
<figcaption>click to play</figcaption>
<audio controls src="aa.mp3">
        Your browser does not support the
        <code>audio</code> element.
</audio>
</figure>
---
format:
  html:
    resources:
      - aa.mp3
---

Fonts

in _quarto.yml(for all page) or header(only for this page)

Code
_quarto.yml
format:
  html:
    mainfont:Times New Roman
    font-size: 120%;
  
# can find more font:https://developer.mozilla.org/en-US/docs/Web/CSS/font-family

source:https://quarto.org/docs/reference/formats/context.html

in page

Code
<p style="font-family: times, serif; font-size:500% ">This is font size 500%</p>

This is font size 500%

Code
<p style="font-family: times, serif; font-size:50% ">This is font size 50%</p>

This is font size 50%

Theme

in _quarto.yml(for all page) or header(only for this page)

Code
_quarto.yml
theme:
  light: cosmo
  dark: [cosmo, theme-dark.scss]

source:https://quarto.org/docs/output-formats/html-themes.html

Managing Execution

render a folder or file

Code
Terminal
quarto render introduction.qmd
quarto render subdir/

Freeze

You can use the freeze option to denote that computational documents should never be re-rendered during a global project render, or alternatively only be re-rendered when their source file changes:

in _quarto.yml(for all page) or header(only for this page)

Code
_quarto.yml
execute:
  freeze: true  # never re-render during project render
Code
_quarto.yml
execute:
  freeze: auto  # re-render only when source changes

Cache

You can use the cache option to cache the results of computations (using the knitr cache for R documents, and Jupyter Cache for Jupyter documents):

Code
_quarto.yml
execute:
  cache: true

Note that cache invalidation is triggered by changes in chunk source code (or other cache attributes you’ve defined). You may however need to manually refresh the cache if you know that some other input (or even time) has changed sufficiently to warrant an update. To do this, render either individual files or an entire project using the –cache-refresh option:

source:https://quarto.org/docs/projects/code-execution.html

Code
Terminal
quarto render mydoc.qmd --cache-refresh # single doc
quarto render --cache-refresh           # entire project

add comment

using giscus comment tool

https://giscus.app/

step 1 install giscus on github

https://github.com/apps/giscus

step 2 enable GitHub Discussions for your repository

step 3 add script to blog post

Code
'''
<script src="https://giscus.app/client.js"
        data-repo="[ENTER REPO HERE]"
        data-repo-id="[ENTER REPO ID HERE]"
        data-category="[ENTER CATEGORY NAME HERE]"
        data-category-id="[ENTER CATEGORY ID HERE]"
        data-mapping="pathname"
        data-strict="0"
        data-reactions-enabled="1"
        data-emit-metadata="0"
        data-input-position="bottom"
        data-theme="preferred_color_scheme"
        data-lang="en"
        crossorigin="anonymous"
        async>
</script>
'''

step 3b or using comment for all page

add into _quarto.yml

Code
_quarto.yml
format:
  html:
    include-after-body: 
         - text: <script src="https://giscus.app/client.js" data-repo="[ENTER REPO HERE]" data-repo-id="[ENTER REPO ID HERE]" data-category="[ENTER CATEGORY NAME HERE]" data-category-id="[ENTER CATEGORY ID HERE]" data-mapping="pathname" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="bottom" data-theme="preferred_color_scheme" data-lang="en" crossorigin="anonymous" async> </script>

quarto engine

Quarto is a multi-language, next generation version of R Markdown from RStudio, with many new features and capabilities

by defalut,Quarto uses Knitr to execute R code

Code
title: "matplotlib demo"
format:
  html:
    code-fold: true

when set jupyter: python3 it will run quarto file with python

First, install jupyter & rpy2

Code
pip install jupyter
pip install rpy2

add engine jupyter: python3

Code
---
title: "ggplot2 demo"
author: "Norah Jones"
date: "5/22/2021"
format: 
  html:
    code-fold: true
jupyter: python3
---

Note that when rendering an .ipynb Quarto will not execute the cells within the notebook by default (the presumption being that you have already executed them while editing the notebook)

You can also specify this behavior within the notebook’s YAML front matter:

Code
---
title: "My Notebook"
execute: 
  enabled: true
---

Run R in python code chunk under python environment

Code
%load_ext rpy2.ipython
Code
%%R
print(R.version)

quarto output format

Code
---
title: "ggplot2 demo"
author: "Tony D"
date: "5/22/2021"
format: 
  html
---
Code
---
title: "ggplot2 demo"
author: "Tony D"
date: "5/22/2021"
format: 
  pdf
---
Code
---
title: "ggplot2 demo"
author: "Tony D"
date: "5/22/2021"
format: 
  docx
---
Code
---
title: "ggplot2 demo"
author: "Tony D"
date: "5/22/2021"
format: 
  pptx
---
Code
---
title: "ggplot2 demo"
author: "Tony D"
date: "5/22/2021"
format: 
  revealjs
---
Code
---
title: "ggplot2 demo"
author: "Tony D"
date: "5/22/2021"
format: 
  html: default
  revealjs: default
---

Reference

https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf

Back to top